<template>
  <div>
    <h1>维修申请</h1>
    <el-card>
      <el-form :model="repairForm" label-width="120px" :rules="rules" ref="repairFormRef">
        <el-form-item label="故障描述" prop="description">
          <el-input
            v-model="repairForm.description"
            type="textarea"
            :rows="4"
            placeholder="请详细描述故障情况"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitRepair">提交维修申请</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { addRepairorder } from '@/api/repairorder'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/store/user'

const userStore = useUserStore()

const repairForm = ref({
  description: ''
})

const rules = {
  description: [{ required: true, message: '请输入故障描述', trigger: 'blur' }]
}

const repairFormRef = ref(null)

const submitRepair = () => {
  repairFormRef.value.validate(valid => {
    if (valid) {
      const formData = {
        ...returnForm.value,
        userId: userStore.userId
      }
      addRepairorder(formData).then(() => {
        ElMessage.success('维修申请提交成功')
        resetForm()
      })
    }
  })
}

const resetForm = () => {
  repairFormRef.value.resetFields()
}
</script>

<style scoped>
.el-card {
  margin-top: 20px;
}
.el-form {
  width: 600px;
}
</style>